<?php

class Header extends Controller {

    public function __construct() {
        parent::Controller();
        if (!$this->session->userdata('isAuthenticated')) {
            redirect(base_url().'admin.php?c=auth&m=index');
        }
        $this->load->model('headermodel', 'header');
        $this->load->model('photomodel', 'photo');
    }

    public function index() {
        if ($header = $this->header->getHeader()) {
            $data['title'] = '主任简介';
            $data['header'] = $header;
            $this->load->view('header/index', $data);
        } else {
            redirect(base_url().'/admin.php?c=header&m=create');
        }
    }

    public function create() {
        if ($this->header->getHeader()) {
            redirect(base_url().'/admin.php?c=header&m=edit');
        } else {
            $this->load->helper('form');
            $data['photos'] = $this->getPhotos();
            $data['sex_array'] = $this->getSexArray();
            $this->load->view('header/create', $data);
        }
    }

    public function add() {
        if ($this->header->getHeader()) {
            redirect(base_url().'/admin.php?c=header&m=edit');
        } else {
            $this->load->library('form_validation');
			
			$this->form_validation->set_rules('name', '姓名', 'required');
			$this->form_validation->set_rules('content', '个人简介', 'required');
			
            if ($this->form_validation->run() == false) {
                $data['photos'] = $this->getPhotos();
                $data['sex_array'] = $this->getSexArray();
                $this->load->view('header/create', $data);
            } else {
                $header['type'] = 0;
                $header['photo_id'] = $this->input->post('photo');
                $header['name'] = $this->input->post('name');
                $header['sex'] = $this->input->post('sex');
                $header['content'] = $this->input->post('content');
                $header['created_at'] = date('Y-m-d H:i:s', time());
                if ($this->db->insert('lawyer', $header)) {
                    $this->session->set_flashdata('notice', '添加成功！');
                    redirect(base_url().'/admin.php?c=header&m=index');
                } else {
                    $this->session->set_flashdata('error', '添加失败，数据库错误！');
                    redirect(base_url().'/admin.php?c=header&m=create');
                }
            }
        }
    }

    public function edit() {
        $this->load->helper('form');
        if ($header = $this->header->getHeader()) {
            $data['header'] = $header;
            $data['photos'] = $this->getPhotos();
            $data['sex_array'] = $this->getSexArray();
            $this->load->view('header/edit', $data);
        } else {
            show_404();
        }
    }

    public function update() {
        if ($header = $this->header->getHeader()) {
            $this->load->library('form_validation');
			$this->form_validation->set_rules('name', '姓名', 'required');
			$this->form_validation->set_rules('content', '个人简介', 'required');
            if ($this->form_validation->run() == false) {
                $data['sex_array'] = $this->getSexArray();
                $data['photos'] = $this->getPhotos();
                $data['header'] = $header;
                $this->load->view('header/edit', $data);
            } else {
                $h['photo_id'] = $this->input->post('photo');
                $h['name'] = $this->input->post('name');
                $h['sex'] = $this->input->post('sex');
                $h['content'] = $this->input->post('content');
                if ($this->db->update('lawyer', $h, array('id' => $header->id, 'type' => 0))) {
                    $this->session->set_flashdata('notice', '修改成功！');
                    redirect(base_url().'/admin.php?c=header&m=index');
                } else {
                    $this->session->set_flashdata('error', '修改失败，数据库错误！');
                    redirect(base_url().'/admin.php?c=header&m=edit');
                }
            }
        } else {
            show_404();
        }
    }

    private function getPhotos() {
        $photos[0] = '>> select <<';
        $query = $this->db->get('photo');
        foreach ($query->result() as $photo) {
            $photos[$photo->id] = $photo->label;
        }
        return $photos;
    }

    private function getSexArray() {
        return array(
            '0' => '女',
            '1' => '男'
        );
    }

}